/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-03-14     YHJ       the first version
 */
#include <rtthread.h>
#include "spi_flash.h"
#include "spi_flash_sfud.h"
#include "drv_spi.h"
#include "dfs_fs.h"
#include <dfs_file.h>
#if defined(BSP_USING_SPI1)

#define FS_PARTITION_NAME             "W25Q128"
#define FS_TYPE                         "elm"
static int rt_hw_spi_flash_init(void)
{
    __HAL_RCC_GPIOA_CLK_ENABLE();
    rt_thread_delay(1000);
    rt_hw_spi_device_attach("spi1", "spi10", GPIOA, GPIO_PIN_4);
    if (RT_NULL == rt_sfud_flash_probe(FS_PARTITION_NAME, "spi10"))
    {
        return -RT_ERROR;
    }

    return RT_EOK;
}
INIT_COMPONENT_EXPORT(rt_hw_spi_flash_init);



static int onboard_spiflash_mount(void)
{

    if (dfs_mount(FS_PARTITION_NAME, "/", FS_TYPE, 0, 0) == RT_EOK)
    {
        LOG_I("spi flash mount to '/'");
    }
    else
    {
        int ret = dfs_mkfs(FS_TYPE, FS_PARTITION_NAME);
        if ((ret = dfs_mount(FS_PARTITION_NAME, "/",FS_TYPE, 0, 0)) == RT_EOK)
        {
            LOG_I("spi flash mount to '/' ret %d\n",ret);
        }
        else
        {
            LOG_E("spi flash failed to mount to '/' ret %d\n",ret);
        }
    }

    return RT_EOK;
}

INIT_APP_EXPORT(onboard_spiflash_mount);
#endif

